// api/session_api/session_remove.go
package session_api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"muziai-server/global"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

func (SessionApi) SessionRemoveView(c *gin.Context) {
	var cr models.IDListRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithError(c, err)
		return
	}

	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)

	query := global.DB.Where("")
	if claims.Role == ctype.AuthRole {
		query.Where("user_id = ?", claims.UserID)
	}
	var sessionList []models.SessionModel
	global.DB.Where(query).Find(&sessionList, "id in ?", cr.IDList)

	global.DB.Delete(&sessionList)

	res.OkWithMsg(c, fmt.Sprintf("删除会话%d条，成功%d条", len(cr.IDList), len(sessionList)))
}
